package com.jukte.recipe;

import com.ewhat.recipes.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class HomeTabActivity extends BaseActivity  implements OnClickListener{

	TextView tv_tab1,tv_tab2,tv_tab3;
	
	int tab_index;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home_tab);
		
		super.init();
		
		tv_tab1 = (TextView) findViewById(R.id.tv_tab1);
		tv_tab2 = (TextView) findViewById(R.id.tv_tab2);
		tv_tab3 = (TextView) findViewById(R.id.tv_tab3);
		
		tv_tab1.setOnClickListener(this);
		tv_tab2.setOnClickListener(this);
		tv_tab3.setOnClickListener(this);
		
		setView(new RecipeTabFragment());
		tv_tab1.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selected)); //  ImageResource(R.drawable.icon_button1_press);
		tab_index=0;
		
		
		/*initialiseTabHost(savedInstanceState);
		
		if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
        }*/
	}

	@Override
	public void onClick(View v) {
		tv_tab1.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_unselected));
		tv_tab2.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_unselected));
		tv_tab3.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_unselected));
		
		switch(v.getId()){
		case R.id.tv_tab1:
			setView(new RecipeTabFragment());
			tab_index=0;
			tv_tab1.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selected));
			break;
		case R.id.tv_tab2:
			setView(new AboutTabFragment());
			tab_index=1;
			tv_tab2.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selected));
			break;
		case R.id.tv_tab3:
			setView(new ShareTabFragment());
			tab_index=2;
			tv_tab3.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_selected));
			break;
		}
	}
	
	private void setView(final Fragment fragment){
		getSupportFragmentManager().beginTransaction()
		.replace(R.id.content, fragment)
		.commit();
	}

	
	
	
	
	
	
	
	/*private TabHost mTabHost;
	private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, HomeTabActivity.TabInfo>();
	private TabInfo mLastTab = null;
	private TextView titleText;
	
	
	private class TabInfo {
		private String tag;
        private Class<?> clss;
        private Bundle args;
        private Fragment fragment;
        TabInfo(String tag, Class<?> clazz, Bundle args) {
       	 this.tag = tag;
       	 this.clss = clazz;
       	 this.args = args;
        }
		
	}
	
	
	class TabFactory implements TabContentFactory{
		private final Context mContext;

	    public TabFactory(Context context) {
	        mContext = context;
	    }

	    public View createTabContent(String tag) {
	        View v = new View(mContext);
	        v.setMinimumWidth(0);
	        v.setMinimumHeight(0);
	        return v;
	    }
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home_tab);
		
		init();
		
		initialiseTabHost(savedInstanceState);
		
		if (savedInstanceState != null) {
            mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
        }
	}
	
	@Override
	public void init() {
		super.init();
	}
	
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
        super.onSaveInstanceState(outState);
    }
	
	
	private void initialiseTabHost(Bundle args) {
		mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        TabInfo tabInfo = null;
        HomeTabActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec(Constants.RECIEPE_TAB_TAG).setIndicator(getIndicatorView("Recipe Menu", R.drawable.tab_reciepe)), ( tabInfo = new TabInfo(Constants.RECIEPE_TAB_TAG, RecipeTabFragment.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        HomeTabActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec(Constants.ABOUT_TAB_TAG).setIndicator(getIndicatorView("About Quinoa", R.drawable.tab_about)), ( tabInfo = new TabInfo(Constants.ABOUT_TAB_TAG, AboutQuinoaTabFragment.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        HomeTabActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec(Constants.SHARE_TAB_TAG).setIndicator(getIndicatorView("Share", R.drawable.tab_share)), ( tabInfo = new TabInfo(Constants.SHARE_TAB_TAG, ShareTabFragment.class, args)));
        this.mapTabInfo.put(tabInfo.tag, tabInfo);
        
        this.onTabChanged(Constants.RECIEPE_TAB_TAG);
        mTabHost.setOnTabChangedListener(this);
	}

	
	private View getIndicatorView(String text,int drawableId)
	{
		View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, this.mTabHost.getTabWidget(), false);
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(text);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(drawableId);
		return tabIndicator;
	}
	
	private static void addTab(HomeTabActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
		// Attach a Tab view factory to the spec
		tabSpec.setContent(activity.new TabFactory(activity));
        String tag = tabSpec.getTag();

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
        if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
            FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
            ft.detach(tabInfo.fragment);
            ft.commit();
            activity.getSupportFragmentManager().executePendingTransactions();
        }

        tabHost.addTab(tabSpec);
	}
	
	public void onTabChanged(String tag) {
		// TODO Auto-generated method stub
		TabInfo newTab = this.mapTabInfo.get(tag);
		if (mLastTab != newTab) {
			FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                	ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(this,
                            newTab.clss.getName(), newTab.args);
                    ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);
                    
                }
            }

            mLastTab = newTab;
            //ft.addToBackStack(null);
            ft.commit();
            this.getSupportFragmentManager().executePendingTransactions();
		}
	}*/

}
